package com.yu.demo.user.domain.entity;

import com.yu.demo.user.domain.valueobject.*;
import lombok.Getter;

import java.time.LocalDateTime;

/**
 * 用户领域实体
 * 封装用户的核心业务逻辑和状态
 */
@Getter
public class User {
    private UserId id;
    private Username username;
    private Email email;
    private PasswordHash passwordHash;
    private UserRole role;
    private boolean isActive;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    private LocalDateTime lastLogin;

    // 私有构造函数，通过工厂方法创建
    private User() {}

    /**
     * 创建新用户
     */
    public static User create(Username username, Email email, PasswordHash passwordHash, UserRole role) {
        User user = new User();
        user.username = username;
        user.email = email;
        user.passwordHash = passwordHash;
        user.role = role;
        user.isActive = true;
        user.createdAt = LocalDateTime.now();
        user.updatedAt = LocalDateTime.now();
        return user;
    }

    /**
     * 从持久化数据重建用户实体
     */
    public static User reconstruct(UserId id, Username username, Email email, 
                                 PasswordHash passwordHash, UserRole role, 
                                 boolean isActive, LocalDateTime createdAt, 
                                 LocalDateTime updatedAt, LocalDateTime lastLogin) {
        User user = new User();
        user.id = id;
        user.username = username;
        user.email = email;
        user.passwordHash = passwordHash;
        user.role = role;
        user.isActive = isActive;
        user.createdAt = createdAt;
        user.updatedAt = updatedAt;
        user.lastLogin = lastLogin;
        return user;
    }

    /**
     * 更新用户信息
     */
    public void updateProfile(Username username, Email email) {
        if (username != null) {
            this.username = username;
        }
        if (email != null) {
            this.email = email;
        }
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 更新密码
     */
    public void changePassword(PasswordHash newPasswordHash) {
        this.passwordHash = newPasswordHash;
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 更新用户角色
     */
    public void changeRole(UserRole newRole) {
        this.role = newRole;
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 激活用户
     */
    public void activate() {
        this.isActive = true;
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 停用用户
     */
    public void deactivate() {
        this.isActive = false;
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 记录登录时间
     */
    public void recordLogin() {
        this.lastLogin = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 检查用户是否为管理员
     */
    public boolean isAdmin() {
        return role.isAdmin();
    }

    /**
     * 检查用户是否激活
     */
    public boolean isActiveUser() {
        return isActive;
    }

    /**
     * 检查用户是否可以登录
     */
    public boolean canLogin() {
        return isActive;
    }
}
